<?php
$section = "booking";
$titre = _LB_BOOKING_PREVIEW;
$link = $section;

$act = Request::getIntNumber('act');
$sesID = Request::getIntNumber('sesID');
//if action = UPDATE or CREATE
if ($act == ACT_VALID_EDIT)
{
	$memberID = Request::getIntNumber('member');
	if($memberID == 0){
		redirect(APPLICATION_URL);
	}
	$bookingData = unserialize($_SESSION['adm_bookingData']);
	if(!is_array($bookingData)){
		redirect(APPLICATION_URL);
	}
	$booked = array();
	$booking = new Booking();
	$booking->ID_MEMBER = $memberID;
	$booking->DATE_CREATION = date("Y-m-d H:i:s");
	$booking->IS_REMINDED = 0;
	$booking->STATUS = 1;
	if($booking->insert()){
		foreach($bookingData as $data){
			$time = $data["TIME"];
			$date = $data["DATE"];
			$booking_date = $date. " {$time}";
			$booking_detail = new Booking_detail();
			$booking_detail->ID_BOOKING = $booking->ID;
			$booking_detail->BOOKING_DATE = $booking_date;
			$booking_detail->ID_TECHNICIAN = 0;
			if($booking_detail->insert()){
				foreach($data['SERVICES'] as $serviceID){
					$booking_detail_service = new Booking_detail_service();
					$booking_detail_service->ID_BOOKING_DETAIL = $booking_detail->ID;
					$booking_detail_service->ID_SERVICE_BOOKING = $serviceID;
					$booking_detail_service->insert();
				}
			}
		}
		$notify = new Mail_Notify();
		$notify->ID_BOOKING = $booking->ID;
		$notify->STATUS = 1;
		$notify->insert();
		$_SESSION['adm_bookingData'] = NULL;
		unset($_SESSION['adm_bookingData']);
		redirect(APPLICATION_URL."/booking/detail/?id=". $booking->ID);
	}
}
else if($act == ACT_DELETE){
	if( isset($_SESSION['adm_bookingData']) && $_SESSION['adm_bookingData'] != ""){
		$bookingData = unserialize($_SESSION['adm_bookingData']);
		if(!is_array($bookingData)){
			redirect(APPLICATION_URL_FO);
		}
		
		if(isset($bookingData[$sesID])){
			unset($bookingData[$sesID]);
			$tmp = array();
			$count = 0;
			foreach($bookingData as $data){
				$tmp[$count] = $data;
				$count++;
			}
			$_SESSION['adm_bookingData'] = serialize($tmp);
			redirect(APPLICATION_URL."/booking/preview/");
		}
	}
}
$page = new Static_Page();
$page->query("SELECT * FROM STATIC_PAGE WHERE CODE = 'booking_preview'");
$page->fetch();
$titre = $page->TITLE;
$service = new Service_Booking();
if($_SESSION['adm_bookingData'] == ""){
	
}
$bookingData = unserialize($_SESSION['adm_bookingData']);
if(!is_array($bookingData)){
	redirect(APPLICATION_URL);
}
$booked = array();
foreach($bookingData as $data){
	$sql = "SELECT * FROM SERVICE_BOOKING WHERE ID IN (".implode(",", $data['SERVICES']).")";
	$service->query($sql);
	$serv = array();
	while($service->fetch()){
		$serv[] = array(
			"TITLE" => $service->TITLE,
			"TIME" => $service->TIME,
			"PRICE" => $service->PRICE
		);
	}
	$bookingDate = $data["DATE"]. " ".$data['TIME'];
	$booked[] =  array(
		"services" => $serv,
		"bookingDate" => $bookingDate
	);
}
$members = new Member();
$members->DISABLED = 0;
$members->find();
//create cfer
$cfer = new Cfer(array(
	_LB_ACCUEIL =>  APPLICATION_URL_FO."/",
    _LB_BOOKING_MANAGEMENT => APPLICATION_URL."/booking/list/",
	_LB_BOOKING_PREVIEW => APPLICATION_URL."/booking/preview/"
));

Session::setBackCfer($link, $cfer->getCferArray());
$smarty->assign('cfer', $cfer);
$smarty->assign('booked', $booked);
$smarty->assign('services', $service);
$smarty->assign('members', $members);
$smarty->assign('page', $page);
$smarty->assign('titre', $titre);
$smarty->display("booking_preview.html");
?>